<?php

namespace App\Http\Controllers\API;

use App\Http\Requests\API\CreateItemOrderAPIRequest;
use App\Http\Requests\API\UpdateItemOrderAPIRequest;
use App\Models\ItemOrder;
use App\Repositories\ItemOrderRepository;
use Illuminate\Http\Request;
use App\Http\Controllers\AppBaseController;
use InfyOm\Generator\Criteria\LimitOffsetCriteria;
use Prettus\Repository\Criteria\RequestCriteria;
use Response;

/**
 * Class ItemOrderController
 * @package App\Http\Controllers\API
 */

class ItemOrderAPIController extends AppBaseController
{
    /** @var  ItemOrderRepository */
    private $itemOrderRepository;

    public function __construct(ItemOrderRepository $itemOrderRepo)
    {
        $this->itemOrderRepository = $itemOrderRepo;
    }

    /**
     * @param Request $request
     * @return Response
     *
     * @SWG\Get(
     *      path="/itemOrders",
     *      summary="Get a listing of the ItemOrders.",
     *      tags={"ItemOrder"},
     *      description="Get all ItemOrders",
     *      produces={"application/json"},
     *      @SWG\Response(
     *          response=200,
     *          description="successful operation",
     *          @SWG\Schema(
     *              type="object",
     *              @SWG\Property(
     *                  property="success",
     *                  type="boolean"
     *              ),
     *              @SWG\Property(
     *                  property="data",
     *                  type="array",
     *                  @SWG\Items(ref="#/definitions/ItemOrder")
     *              ),
     *              @SWG\Property(
     *                  property="message",
     *                  type="string"
     *              )
     *          )
     *      )
     * )
     */
    public function index(Request $request)
    {
        $this->itemOrderRepository->pushCriteria(new RequestCriteria($request));
        $this->itemOrderRepository->pushCriteria(new LimitOffsetCriteria($request));
        $itemOrders = $this->itemOrderRepository->all();

        return $this->sendResponse($itemOrders->toArray(), trans('flash.api.retrieve_success', ['model' => 'Item Orders']));
    }

    /**
     * @param CreateItemOrderAPIRequest $request
     * @return Response
     *
     * @SWG\Post(
     *      path="/itemOrders",
     *      summary="Store a newly created ItemOrder in storage",
     *      tags={"ItemOrder"},
     *      description="Store ItemOrder",
     *      produces={"application/json"},
     *      @SWG\Parameter(
     *          name="body",
     *          in="body",
     *          description="ItemOrder that should be stored",
     *          required=false,
     *          @SWG\Schema(ref="#/definitions/ItemOrder")
     *      ),
     *      @SWG\Response(
     *          response=200,
     *          description="successful operation",
     *          @SWG\Schema(
     *              type="object",
     *              @SWG\Property(
     *                  property="success",
     *                  type="boolean"
     *              ),
     *              @SWG\Property(
     *                  property="data",
     *                  ref="#/definitions/ItemOrder"
     *              ),
     *              @SWG\Property(
     *                  property="message",
     *                  type="string"
     *              )
     *          )
     *      )
     * )
     */
    public function store(CreateItemOrderAPIRequest $request)
    {
        $input = $request->all();

        $itemOrders = $this->itemOrderRepository->create($input);

        return $this->sendResponse($itemOrders->toArray(), trans('flash.api.save_success', ['model' => 'Item Order']));
    }

    /**
     * @param int $id
     * @return Response
     *
     * @SWG\Get(
     *      path="/itemOrders/{id}",
     *      summary="Display the specified ItemOrder",
     *      tags={"ItemOrder"},
     *      description="Get ItemOrder",
     *      produces={"application/json"},
     *      @SWG\Parameter(
     *          name="id",
     *          description="id of ItemOrder",
     *          type="integer",
     *          required=true,
     *          in="path"
     *      ),
     *      @SWG\Response(
     *          response=200,
     *          description="successful operation",
     *          @SWG\Schema(
     *              type="object",
     *              @SWG\Property(
     *                  property="success",
     *                  type="boolean"
     *              ),
     *              @SWG\Property(
     *                  property="data",
     *                  ref="#/definitions/ItemOrder"
     *              ),
     *              @SWG\Property(
     *                  property="message",
     *                  type="string"
     *              )
     *          )
     *      )
     * )
     */
    public function show($id)
    {
        /** @var ItemOrder $itemOrder */
        $itemOrder = $this->itemOrderRepository->findWithoutFail($id);

        if (empty($itemOrder)) {
            return $this->sendError(trans('flash.api.not_found', ['model'=>'Item Order']), 404);
        }

        return $this->sendResponse($itemOrder->toArray(), trans('flash.api.retrieve_success', ['model' => 'Item Order']));
    }

    /**
     * @param int $id
     * @param UpdateItemOrderAPIRequest $request
     * @return Response
     *
     * @SWG\Put(
     *      path="/itemOrders/{id}",
     *      summary="Update the specified ItemOrder in storage",
     *      tags={"ItemOrder"},
     *      description="Update ItemOrder",
     *      produces={"application/json"},
     *      @SWG\Parameter(
     *          name="id",
     *          description="id of ItemOrder",
     *          type="integer",
     *          required=true,
     *          in="path"
     *      ),
     *      @SWG\Parameter(
     *          name="body",
     *          in="body",
     *          description="ItemOrder that should be updated",
     *          required=false,
     *          @SWG\Schema(ref="#/definitions/ItemOrder")
     *      ),
     *      @SWG\Response(
     *          response=200,
     *          description="successful operation",
     *          @SWG\Schema(
     *              type="object",
     *              @SWG\Property(
     *                  property="success",
     *                  type="boolean"
     *              ),
     *              @SWG\Property(
     *                  property="data",
     *                  ref="#/definitions/ItemOrder"
     *              ),
     *              @SWG\Property(
     *                  property="message",
     *                  type="string"
     *              )
     *          )
     *      )
     * )
     */
    public function update($id, UpdateItemOrderAPIRequest $request)
    {
        $input = $request->all();

        /** @var ItemOrder $itemOrder */
        $itemOrder = $this->itemOrderRepository->findWithoutFail($id);

        if (empty($itemOrder)) {
            return $this->sendError(trans('flash.api.not_found', ['model'=>'Item Order']), 404);
        }

        $itemOrder = $this->itemOrderRepository->update($input, $id);

        return $this->sendResponse($itemOrder->toArray(), trans('flash.api.update_success', ['model' => 'ItemOrder']));
    }

    /**
     * @param int $id
     * @return Response
     *
     * @SWG\Delete(
     *      path="/itemOrders/{id}",
     *      summary="Remove the specified ItemOrder from storage",
     *      tags={"ItemOrder"},
     *      description="Delete ItemOrder",
     *      produces={"application/json"},
     *      @SWG\Parameter(
     *          name="id",
     *          description="id of ItemOrder",
     *          type="integer",
     *          required=true,
     *          in="path"
     *      ),
     *      @SWG\Response(
     *          response=200,
     *          description="successful operation",
     *          @SWG\Schema(
     *              type="object",
     *              @SWG\Property(
     *                  property="success",
     *                  type="boolean"
     *              ),
     *              @SWG\Property(
     *                  property="data",
     *                  type="string"
     *              ),
     *              @SWG\Property(
     *                  property="message",
     *                  type="string"
     *              )
     *          )
     *      )
     * )
     */
    public function destroy($id)
    {
        /** @var ItemOrder $itemOrder */
        $itemOrder = $this->itemOrderRepository->findWithoutFail($id);

        if (empty($itemOrder)) {
            return $this->sendError('Item Order not found', 404);
        }

        $itemOrder->delete();

        return $this->sendResponse($id, trans('flash.api.delete_success', ['model' => 'Item Order']));
    }
}
